﻿using MixingStation.LTWebService;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MixingStation
{
    public partial class AdminConfirmForm : Form
    {
        private string m_Title = "";
        public string Title
        {
            set { m_Title = value; }
        }

        private string m_LoginName = "";
        public string LoginName 
        { 
            get { return m_LoginName; }
        }

        private userWs m_User = new userWs ();
        public userWs User
        {
            get { return m_User; }
        }

        //是否联网
        private bool m_IsOnline = true;
        public bool IsOnline
        {
            set { m_IsOnline = value; }
        }

        //用户权限名称
        private string m_UserType = "";
        public string UserType
        {
            get { return m_UserType; }
            set { m_UserType = value; }
        }

        public AdminConfirmForm()
        {
            InitializeComponent();
        }

        private void AdminConfirmForm_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(m_Title))
                this.Text = "管理员确认";
            else
                this.Text = "管理员确认（" + m_Title + ")";
            InitCmbUser();
        }

        //初始化用户下拉框
        private void InitCmbUser()
        {
            string users = ConfigurationManager.AppSettings["UserNames"].ToString();
            string[] userNameArr = users.Split(',');
            for (int i = 0; i < userNameArr.Length; i++)
                cmbUser.Properties.Items.Add(userNameArr[i]);
            cmbUser.SelectedIndex = 0;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            string name = this.cmbUser.Text.Trim();
            string password = this.txtPassword.Text.Trim();

            //联网状态下调用系统确认是否有权限，断网情况下只要根据本地维护的确认
            if (m_IsOnline)
            {
                if (!ConfirmByOnline(name, password))
                    return;
            }
            else
            {
                if (!ConfirmByUnline(name, password))
                    return;
            }
            m_LoginName = m_User.employeeName;
            this.DialogResult = DialogResult.OK;
        }

        //联网状态下确认
        private bool ConfirmByOnline(string name, string password)
        {
            try
            {
                using (LTCardAPIServiceClient client = new LTCardAPIServiceClient())
                {
                    //获取用户
                    m_User = client.login(name, password);
                    //获取该用户下的权限
                    if (m_User != null)
                    {
                        menuWs[] menuArr = client.getMenuWs(long.Parse(m_User.id));
                        if (menuArr.Length == 0 || !ChenkMenu(menuArr, m_UserType))
                        {
                            MessageBox.Show(this, "该用户没有权限!");
                            return false;
                        }
                    }
                    else
                    {
                        MessageBox.Show(this, "用户名或密码错误，请重新输入!");
                        return false;
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.error("登录异常：" + ex.ToString());
                MessageBox.Show("登录异常:" + ex.Message);
                return false;
            }
            return true;
        }

        private bool ChenkMenu(menuWs[] menu, string menuname)
        {
            bool res = false;
            for (int i = 0; i < menu.Length; i++)
            {
                int index = menu[i].name.IndexOf(menuname);
                if (index == 0 || menu[i].name == "管理员")
                {
                    LogHelper.info("拥有权限菜单名称：" + menu[i].name + "menuName:" + menuname);
                    res = true;
                }
            }
            return res;
        }

        private bool ConfirmByUnline(string name, string password)
        {
            try
            {
                string sql = "select * from [User] where UserName = '" + name + "' and Password = '" + password + "'";
                DataTable dt = CommonHelper.DoSelectOpreation(sql);
                if (dt == null || dt.Rows.Count == 0)
                {
                    MessageBox.Show(this, "用户名或密码错误，请重新输入!");
                    return false;
                }
                if (dt.Rows[0]["IsAdmin"].ToString().ToUpper() != "TRUE")
                {
                    MessageBox.Show(this, "该用户不具有管理员权限!");
                    return false;
                }
            }
            catch (Exception ex)
            {
                LogHelper.error("登录异常：" + ex.ToString());
                MessageBox.Show("登录异常:" + ex.Message);
                return false;
            }
            return true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

    }
}
